Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move subscription methods up a level #618

Merged

Conversation

stephenplusplus
Copy link
Contributor

Fixes #605

@stephenplusplus stephenplusplus added api: pubsub Issues related to the Pub/Sub API. don't merge labels May 20, 2015
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label May 20, 2015
*
* @throws {Error} If a name is not provided.
*
* @param {string} name - The name of the subscription.

This comment was marked as spam.

This comment was marked as spam.

@ryanseys
Copy link
Contributor

Different than what I had in mind.

I was thinking more like:

// topic is in options
pubsub.subscribe('my-sub', { topic: pubsub.topic('some-topic') }, callback);

Thinking now, pubsub.subscribe('sub-name', topicObj, options, callback) makes more sense because its not an option in this case BUT a topic name is specified by its project and topic name like so: 'projects/project-id/topics/topic-name' so with your string version here, that wouldn't be the best unless that assumes that the topic is in the same project. It would be better to pass in a whole topic object.

Here's a partial attempt I made a while ago: https://github.com/ryanseys/gcloud-node/commit/78c4cfe6b06a7dcc1d44e226b00dbbc51a31d096 don't let it confuse you because it's not complete but something to gander at.

@stephenplusplus
Copy link
Contributor Author

BUT a topic name is specified by its project and topic name like so: 'projects/project-id/topics/topic-name

I'm just getting the topic name (topic-name), then if the user gives a project-id, that project-id is used to make the full topic name, otherwise, use what's on the PubSub instance.

Quick API comparison :)

// ---
// This PR:
// ---
var pubsub = gcloud.pubsub({ projectId: 'grape-spaceship-123' });

// Each of these next two lines do the same thing:
pubsub.topic('some-topic').subscribe('my-sub', callback);
pubsub.subscribe('some-topic', 'new-sub-name', callback);

// Subscribe to a topic from another project.
pubsub.subscribe('another-topic', 'another-new-sub-name', { projectId: 'squishy-butter-334' }, callback);

// ---
// Your Idea:
// ---
var pubsub = gcloud.pubsub({ projectId: 'grape-spaceship-123' });

// Each of these next two lines do the same thing:
pubsub.topic('some-topic').subscribe('my-sub', callback);
pubsub.subscribe('new-sub-name', pubsub.topic('some-topic'), callback);

// Subscribe to a topic from another project.
var anotherPubsub = gcloud.pubsub({ projectId: 'squishy-butter-334' });

pubsub.subscribe('another-new-sub-name', anotherPubsub.topic('another-topic'), callback);

The main difference is requiring a Topic object vs. using explicit string params. I like both. Halp.

@jgeewax
Copy link
Contributor

jgeewax commented May 21, 2015

Couldn't we take bits of both?

var pubsub1 = gcloud.pubsub({ projectId: 'project1' });

// These all do the same thing
pubsub1.topic('some-topic').subscribe('my-sub', callback); // Most common pattern
pubsub1.subscribe('new-sub-name', 'some-topic', callback);  // Uncommon, but just a name implies same project
pubsub1.subscribe('new-sub-name', pubsub1.topic('some-topic'), callback); // Uncommon, most explicit.

// So then, if we want to subscribe to a topic in another project,
// the only way to do that is to create a topic object that knows it's project.
var pubsub2 = gcloud.pubsub({ projectId: 'project2' });
pubsub2.subscribe('new-sub-name', pubsub1.topic('some-topic'), callback);

@stephenplusplus
Copy link
Contributor Author

I'm cool with that. Now just one smaller detail; shouldn't topic name come before subscription?

pubsub.subscribe('new-sub-name', 'some-topic', callback);
// vs.
pubsub.subscribe('some-topic', 'new-sub-name', callback);

I like the latter, just to follow the standard hierarchy "project -> topic -> subscription".

Just for completeness, here's the full sig:

pubsub.subscribe( ( Topic('topic-name') || 'topic-name' ), 'sub-name', [{ autoAck: true, ... }], function(err, subscription, apiResponse) {});

@jgeewax
Copy link
Contributor

jgeewax commented May 21, 2015

SGTM. Since topic is absolutely required, it should definitely be first.

@ryanseys
Copy link
Contributor

Since topic is absolutely required, it should definitely be first.

Aren't both topic and sub-name required? Either order makes sense to me, so whatever you like :)

@jgeewax
Copy link
Contributor

jgeewax commented May 21, 2015

Yea, both required -- topic being higher up in the hierarchy seems like it should come first though. Sorry, didn't articulate that piece too. :P

@ryanseys
Copy link
Contributor

Cool SGTM!

@stephenplusplus
Copy link
Contributor Author

PTAL.

@@ -163,6 +163,123 @@ PubSub.prototype.createTopic = function(name, callback) {
};

/**
* Create a subscription to this topic. You may optionally provide an object to

This comment was marked as spam.

@stephenplusplus stephenplusplus force-pushed the spp--pubsub-level-methods branch 6 times, most recently from 6c95ee7 to 08ed5d4 Compare May 29, 2015 19:39
@stephenplusplus stephenplusplus force-pushed the spp--pubsub-level-methods branch 2 times, most recently from 3c59635 to a4e3243 Compare June 2, 2015 17:01
@stephenplusplus
Copy link
Contributor Author

While Travis takes a year to run this build...

I'm not sure how I can write a system test for this functionality, since we only have access to one project. Unit testing covers all of the changes pretty well, since it was mostly just relocating code. To support subscribing from another project, all we're really doing is passing a different topic string to the API. The unit tests cover that as well. Is that enough?

@ryanseys
Copy link
Contributor

ryanseys commented Jun 3, 2015

I think this should be good. I can go ahead with a code review if you're ready?

@stephenplusplus
Copy link
Contributor Author

Yes, please!


options = options || {};

if (util.is(topic, 'string')) {

This comment was marked as spam.

This comment was marked as spam.

@@ -124,7 +124,7 @@ util.arrayize = arrayize;
*/
function format(template, args) {
return template.replace(/{([^}]*)}/g, function(match, key) {
return args[key] || match;
return key in args ? args[key] : match;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@stephenplusplus
Copy link
Contributor Author

Merge time!

stephenplusplus added a commit that referenced this pull request Jun 5, 2015
@stephenplusplus stephenplusplus merged commit f167223 into googleapis:master Jun 5, 2015
chingor13 pushed a commit that referenced this pull request Aug 22, 2022
#618)

* build(node): add feat in node post-processor to add client library version number in snippet metadata

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Source-Link: googleapis/synthtool@d337b88
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:d106724ad2a96daa1b8d88de101ba50bdb30b8df62ffa0aa2b451d93b4556641
chingor13 pushed a commit that referenced this pull request Aug 22, 2022
#618)

* build(node): add feat in node post-processor to add client library version number in snippet metadata

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Source-Link: googleapis/synthtool@d337b88
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:d106724ad2a96daa1b8d88de101ba50bdb30b8df62ffa0aa2b451d93b4556641
chingor13 pushed a commit that referenced this pull request Aug 22, 2022
#618)

* build(node): add feat in node post-processor to add client library version number in snippet metadata

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Source-Link: googleapis/synthtool@d337b88
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:d106724ad2a96daa1b8d88de101ba50bdb30b8df62ffa0aa2b451d93b4556641
sofisl pushed a commit that referenced this pull request Nov 10, 2022
This PR was generated using Autosynth. 🌈

Synth log will be available here:
https://source.cloud.google.com/results/invocations/1bcecc39-aa5c-40f4-9453-955704c7ca01/targets

- [ ] To automatically regenerate this PR, check this box.

Source-Link: googleapis/synthtool@57c23fa
sofisl pushed a commit that referenced this pull request Nov 10, 2022
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 468790263

Source-Link: googleapis/googleapis@873ab45

Source-Link: googleapis/googleapis-gen@cb6f37a
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiY2I2ZjM3YWVmZjJhMzQ3MmU0MGE3YmJhY2U4YzY3ZDc1ZTI0YmVlNSJ9
@release-please release-please bot mentioned this pull request Nov 10, 2022
sofisl pushed a commit that referenced this pull request Nov 11, 2022
Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
sofisl pushed a commit that referenced this pull request Nov 11, 2022
#618)

* build(node): add feat in node post-processor to add client library version number in snippet metadata

Co-authored-by: Benjamin E. Coe <bencoe@google.com>
Source-Link: googleapis/synthtool@d337b88
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-nodejs:latest@sha256:d106724ad2a96daa1b8d88de101ba50bdb30b8df62ffa0aa2b451d93b4556641
sofisl pushed a commit that referenced this pull request Sep 13, 2023
* feat: support regapic LRO

Use gapic-generator-typescript v2.15.1.

PiperOrigin-RevId: 456946341

Source-Link: googleapis/googleapis@88fd18d

Source-Link: googleapis/googleapis-gen@accfa37
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiYWNjZmEzNzFmNjY3NDM5MzEzMzM1YzY0MDQyYjA2M2MxYzUzMTAyZSJ9

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the Pub/Sub API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add pubsub#subscription, pubsub#subscribe and pubsub#getSubscriptions
5 participants